package com.tsmti.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Package：PACKAGE_NAME
 * @Class：WebserviceUtils
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/7/23 15:32
 * @Company:
 * @Version：
 * @Modified By:
 */
public class WebserviceUtils {
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**输血系统WebService地址*/
    private final static String WEBSERVICE_URL = SystemParameterUtils.getValueByName("BisWebserviceAddress");
    /**返回值类型 json、xml*/
    private final static String WEBSERVICE_RESULTTYPE = SystemParameterUtils.getValueByName("json");

    /***
     * 执行webservice方法
     * @param trackId   交易代码
     * @param content   参数内容(JSON格式)
     * @return
     */
    public static JSONObject runService(String trackId,String orgId,String content){
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        try {
//            "http://192.168.2.109:8083/services/soap?wsdl"
            final Client client = factory.createClient("http://127.0.0.1:8083/services/soap?wsdl");
            //设置超时单位为毫秒
            HTTPConduit http = (HTTPConduit) client.getConduit();
            HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
            //连接超时
            httpClientPolicy.setConnectionTimeout(30000);
            //取消块编码
            httpClientPolicy.setAllowChunking(false);
            //响应超时
            httpClientPolicy.setReceiveTimeout(30000);
            http.setClient(httpClientPolicy);

            Object[] result = client.invoke("runService", new Object[]{trackId,orgId,content,WEBSERVICE_RESULTTYPE});
            String text = (String) result[0];
            System.out.println(text);
            return JSON.parseObject(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 测试代码
     * @param args
     */
    public static void main(String[] args){
        //测试JaxWsDynamicClientFactory
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        final Client client = factory.createClient("http://192.168.2.109:8083/services/soap?wsdl");

        //设置超时单位为毫秒
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        //连接超时
        httpClientPolicy.setConnectionTimeout(3000);
        //取消块编码
        httpClientPolicy.setAllowChunking(false);
        //响应超时
        httpClientPolicy.setReceiveTimeout(3000);
        http.setClient(httpClientPolicy);
        try {
            Object[] result = client.invoke("runService", new Object[]{"TS001","{\"patientId\":\"0984779\",\"stockOutNo\":\"0001\",\"bagCode\":\"654321\"}"});
            String text = (String) result[0];
            System.out.println(text);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //测试HttpClient
//        HttpClient httpClient = new HttpClient();
//        PostMethod method = new PostMethod("http://127.0.0.1:8091/services/rest/runService");
//        NameValuePair[] param = { new NameValuePair("trackId", "TS001"),
//                                        new NameValuePair("content","{\"patientId\":\"0984779\",\"stockOutNo\":\"0001\",\"bagCode\":\"654321\"}")};
//        method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=" + "UTF-8");
//        method.setRequestBody(param);
//        try {
//            //设置连接时间
//            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
//            httpClient.executeMethod(method);
//            JSONObject obj = JSON.parseObject(method.getResponseBodyAsString());
//            System.out.println(obj);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            //关闭连接
//            method.releaseConnection();
//        }
    }
}
